//es6
import config from './config.js'
export default (params) => {
	// //加载中
	// uni.showLoading({
	// 	title: '加载中'
	// })

	return new Promise((resolve, reject) => {
		//执行异步请求
		wx.request({
			url: config.host + params.url,
			data: params.data || {},
			method: params.method || 'GET',
			header: params.header || {
				'Content-Type': 'application/json',
				'authorization': wx.getStorageSync('authorization')
			},
			success: (res) => {
				if (typeof(res.header.authorization) !== "undefined") {
					wx.setStorageSync('authorization', res.header.authorization)
				}

				if (res.data.code === 50035 || res.data.code === 50037) {
					if (typeof(wx.getStorageSync('authorization')) === "undefined") {
						wx.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 2000
						})
					}
					wx.removeStorageSync('authorization')
					wx.reLaunch({
						url: '/pages/login/login'
					})
					reject(new Error(res.data.message))
				} else {
					resolve(res.data)
				}
			},
			fail: (err) => {
				reject(err);
			},
		})
	});
}
